<template>
    <div class="stock-contianer">
        <div class="stock-tray-title">
            <dl>
                <dt>{{dataset.garbage.name}}</dt>
                <dd>
                    <!-- <p>总重量：<span>{{dataset.ibase.allweight}}kg</span></p> -->
                    <p>总重量：<span>{{dataset.ibase.allnumner}}kg</span></p>
                </dd>
            </dl>
        </div>
        <van-list v-model="dataset.loading" :finished="dataset.finished" finished-text="" @load="controllerRequestList">
            <tray-item-list v-for="(item, index) in dataset.items" :key="index" @onSelectOperate="eventOnSelectItemOperate"
                :mode="true"
                :ibase="[]" :dataset="item" :index="index" :allSelect="allSelectState" class="stock-order-list"
            />
        </van-list>
        <!-- <div class="page-bottom-bar">
            <div class="flex-item check-all-select">
                <van-checkbox v-model="allSelectState">全选</van-checkbox>
            </div>
            <div class="flex-item tip-message">已选择 {{ selectItems.length }} 条数据</div>
            <div class="flex-item">
                <span @click="eventClickPopSelectItem" class="bar-button bar-button-green">出库</span>
            </div>
        </div> -->
    </div>
</template>

<script>
    import {List, Checkbox} from 'vant';

    import TrayItemList from '../../components/currency/order-item/normal-list-stock-tray.vue';

    export default {
        name: "ReservoirManageStockTrayDetails",
        components: {
            [List.name]: List, [Checkbox.name]: Checkbox,
            TrayItemList
        },
        data() {
            return {
                allSelectState: false, selectItems: [],
                trayId: 0,
                dataset: {
                    page: 0, limit: 30,
                    loading: false, finished: false,
                    items: [],garbage:{},ibase:{}
                }
            }
        },
        methods: {
            controllerRequestList() {
                var url = '/api/home/tray/orderlist',
                    params = {
                        token: this.$route.query.token,
                        id: this.$route.query.child
                    };

                var detail = [],orderlist = [];
                this.$util.postRequestInterface(url, params, (res) => {
                    this.dataset.garbage = res.data.garbage;
                    this.dataset.ibase.allnumner = res.data.allnumner;
                    this.dataset.ibase.allweight = res.data.allweight;

                    this.trayId = res.data.id;
                    if(res.data.orderlist) {
                        for(let x of res.data.orderlist) {
                            let y = x.garbageall;

                            // for (let i of x.garbage) {
                            //     detail.push({
                            //         goid: i.goid,
                            //         title:i.name,
                            //         // number: i.number != 0 ? parseInt(i.number) : '',
                            //         // weight: i.weight != 0 ? i.weight : '',
                            //         number: y.weighting_num,
                            //         unit: y.name,
                            //         time:this.$moment(x.create_time*1000).format('YYYY-MM-DD'),
                            //     });
                            // }
                            detail.push({
                                goid: x.goid,
                                title: x.garbage[0].name,
                                // number: i.number != 0 ? parseInt(i.number) : '',
                                // weight: i.weight != 0 ? i.weight : '',
                                number: y.weighting_num,
                                unit: 'kg',
                                time:this.$moment(x.create_time*1000).format('YYYY-MM-DD'),
                            });

                            orderlist.push({
                                ordernumne: x.ordernumne,
                                detail: detail
                            });
                            detail = [];
                        }

                        this.dataset.items = orderlist;
                    }
                    this.dataset.finished = true;
                    this.dataset.loading = false;
                });
            },
            buildFormaDataset() {
                var result = [];

                for(let index of this.selectItems) {
                    let x = this.dataset.items[index];
                    
                    for(let y of x.detail) {
                        result.push(y.goid);
                    }
                }

                return result;
            },
            eventClickPopSelectItem() {
                if(this.selectItems.length == 0) {
                    this.$toast('请选择要出库的选项');
                    return;
                }

                var order = window.sessionStorage.getItem('outOrder');
                if(!order) {
                    this.$toast('请选择出库订单');
                    return;
                }

                var url = '/api/home/tray/orderadd',
                    params = {
                        id: this.trayId,
                        ids: JSON.stringify(this.buildFormaDataset().flat()),
                        token: this.$store.state.options.user.token,
                        orderid: order
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    if(res.data) {
                    } else {
                        // 出库失败
                    }
                    // this.$router.push({
                    //     path: '/shop/withdraw', query: {
                    //         ordernumber: res.data.ordernumber
                    //     }
                    // });
                });
            },
            eventOnSelectItemOperate(params) {
                if(params.select) {
                    this.selectItems.push(params.index);
                } else {
                    for(let i in this.selectItems) {
                        if(i == params.index) {
                            this.selectItems.splice(i, 1);
                            break;
                        } 
                    }
                }
            }
        },
        watch: {
            allSelectState(nVal, oVal) {
                this.selectItems = [];
            }
        }
    }
</script>

<style scoped lang="scss">
    @import "../../libs/styles/bottom-fixed-bar.scss";

    .stock-tray-title {
        padding: 0.8rem 1rem;
        border-bottom: 0.4rem solid #F4F4F4;

        dt {
            color: #333333;
            font-size: 1.4rem;
        }

        dd {
            display: flex;

            p {
                font-size: 1.3rem;
                color: #999999;
            }

            p span {
                color: #00CC00;
            }
            // p:last-child{
            //     margin-left: 1rem;
            // }
        }
    }

    .stock-listing {

        li {
            padding: 1.6rem;
            display: flex;
            font-size: 1.4rem;
            border-bottom: 0.4rem solid #F4F4F4;

            p {
                color: #999999;
            }
            
            p:first-child {
                flex: 1;
                color: #333333;
            }

            p:last-child {
                margin-left: 1rem;
            }

            p span {
                color: #00CC00;
            }
        }
    }

    .stock-order-list {
        padding: 0.6rem 1rem 1.6rem;
    }
</style>